package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.interceptor.RequireLongin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redisService.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object detail(TravelQuery qo){
        return JsonResult.success(travelService.query(qo));
    }
    @GetMapping("/input")
    public Object input(String id){
        //通过目的地查出所有的目的地对象集合
        List<Destination> destinations = destinationService.list();
        HashMap<String, Object> map = new HashMap<>();
        if(StringUtils.hasLength(id)){
            //如果存在就塞
            Travel tv = travelService.get(id);
            map.put("tv",tv);
        }
            map.put("dests",destinations);
        return JsonResult.success(map);
    }
    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequireLongin//必须登录之后才操作
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getHeader(token);
        travel.setUserId(userInfo.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }
    @GetMapping("/detail")
    public  Object detail(String id){
        Travel travel = travelService.get(id);
        return  JsonResult.success(travel);
    }
    @RequireLongin//必须登录之后才操作
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travel,@UserParam  UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,travel);
        travel.setUserId(userInfo.getId());
        travelCommentService.save(travel);
        return JsonResult.success();
    }

    //评论的显示
    @GetMapping("/comments")
    public Object comments(String travelId){

        return JsonResult.success(travelCommentService.queryByTraveId(travelId));
    }

}
